//
//  NSDate+XMGExtension.m
//  01-百思不得姐
//
//  Created by xiaomage on 15/7/27.
//  Copyright (c) 2015年 小码哥. All rights reserved.
//

#import "NSDate+XMGExtension.h"

@implementation NSDate (XMGExtension)


-(NSDateComponents*)deltaFrom:(NSDate*)from{
	//日历
	NSCalendar *calendar=[NSCalendar currentCalendar];
	//比较时间
	NSCalendarUnit unit= NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay |NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
	
	NSDateComponents*comps=	[calendar components:unit fromDate:from toDate:self options:0];
	
	//返回
	return comps;
}



/**是否为今年*/
-(BOOL)isThisYear{
	
	//日历
	NSCalendar *calendar = [NSCalendar currentCalendar];
	NSInteger nowYear=[calendar component:NSCalendarUnitYear fromDate:[NSDate date]];
	NSInteger selfYear=[calendar component:NSCalendarUnitYear fromDate:self];
	
	return nowYear == selfYear;
}

//- (BOOL)isToday
//{
//    // 日历
//    NSCalendar *calendar = [NSCalendar currentCalendar];
//    
//    NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay;
//    
//    NSDateComponents *nowCmps = [calendar components:unit fromDate:[NSDate date]];
//    NSDateComponents *selfCmps = [calendar components:unit fromDate:self];
//    
//    return nowCmps.year == selfCmps.year
//    && nowCmps.month == selfCmps.month
//    && nowCmps.day == selfCmps.day;
//}



-(BOOL)isToday{
	
	NSDateFormatter*fmt=[[NSDateFormatter alloc]init];
	fmt.dateFormat=@"yyyy-MM-dd";
NSString*nowString  = [fmt stringFromDate:[NSDate date]];
	NSString*selfString =[ fmt stringFromDate:self];
	
	return [nowString isEqualToString:selfString];
}


-(BOOL)isYesterday{
	
	// 2014-12-31 23:59:59 -> 2014-12-31
	// 2015-01-01 00:00:01 -> 2015-01-01

	//日期格式划类
	NSDateFormatter*fmt=[[NSDateFormatter alloc]init];
	fmt.dateFormat=@"yyyy-MM-dd";
	
	NSDate*nowDate=[fmt dateFromString:[fmt stringFromDate:[NSDate date]]];
	NSDate*selfDate=[fmt dateFromString:[fmt stringFromDate:self]];
	
	NSCalendar*calendar=[NSCalendar currentCalendar];
	NSDateComponents*comps=[calendar components:NSCalendarUnitDay | NSCalendarUnitMonth|NSCalendarUnitYear fromDate:selfDate toDate:nowDate options:0];
	
	return comps.year== 0
	&& comps.month== 0
	&& comps.day== 1;
	
	
}





@end
